package com.markhsiu.io.serialize;

import com.markhsiu.core.constant.Bits;
import com.markhsiu.store.StoreDB;

import io.openmessaging.MessageHeader;
import io.openmessaging.demo.DefaultBytesMessage;
import io.openmessaging.demo.DefaultKeyValue;

public class MessageUtil {
	
	public static String getTopic(DefaultBytesMessage message) {
		return message.headers().getString(MessageHeader.TOPIC);
	}

	public static String getQueue(DefaultBytesMessage message) {
		return message.headers().getString(MessageHeader.QUEUE);
	}
	
	/**
	 * if includeLen == true  : MAGIC+len+message
	 * else message
	 * @param message
	 * @param includeLen
	 * @return
	 */
	public static byte[] buildBytes(DefaultBytesMessage message,boolean includeLen) {
		byte[] headerBytes = message.headers().mapToBytes();
		byte[] propertyBytes = message.properties().mapToBytes();
		byte[] body = message.getBody();
		 
		int offset = 0;
		int len = headerBytes.length + propertyBytes.length + body.length + 4 * 3 ;
		
		byte[] data;
		if(includeLen){
			data = new byte[len+4+1];
			data[offset++] = StoreDB.MAGIC;
			Bits.putInt(data, offset, len);
			offset += 4;
		} else {
			data = new byte[len];
		}
		
 		Bits.putInt(data, offset, headerBytes.length);
		offset += 4;
		System.arraycopy(headerBytes, 0, data, offset, headerBytes.length);
		offset += headerBytes.length;

		Bits.putInt(data, offset, propertyBytes.length);
		offset += 4;
		System.arraycopy(propertyBytes, 0, data, offset, propertyBytes.length);
		offset += propertyBytes.length;

		Bits.putInt(data, offset, body.length);
		offset += 4;
		System.arraycopy(body, 0, data, offset, body.length);
		return data;
	}


	/**
	 * message
	 * @param message
	 * @return
	 */
	public static byte[] buildBytes(DefaultBytesMessage message) {		 
		return buildBytes(message, false);
	}

	public static DefaultBytesMessage restore(byte[] data) {
		int offset = 0;

		// headers
		int length1 = byteToInt(data, offset);
		offset += 4;
		byte[] headerBytes = new byte[length1];
		System.arraycopy(data, offset, headerBytes, 0, length1);
		offset += length1;

		// propertis
		int length2 = byteToInt(data, offset);
		offset += 4;
		byte[] propertyBytes = new byte[length2];
		System.arraycopy(data, offset, propertyBytes, 0, length2);
		offset += length2;

		// body
		int length3 = byteToInt(data, offset);
		offset += 4;
		byte[] body = new byte[length3];
		System.arraycopy(data, offset, body, 0, length3);
		offset += length3;

		DefaultBytesMessage message = new DefaultBytesMessage(body);
		DefaultKeyValue heades = new DefaultKeyValue().bytesToMap(headerBytes);
		message.putAllHeaders(heades);
		if (length2 > 0) {
			DefaultKeyValue properties = new DefaultKeyValue().bytesToMap(propertyBytes);
			message.putAllProperties(properties);
		}

		return message;
	}
 
 

	public static short byteToShort(byte[] data, int offset) {
		return Bits.toShort(data[offset + 0], data[offset + 1]);
	}

	public static int byteToInt(byte[] data, int offset) {
		return Bits.toInt(data[offset + 0], data[offset + 1], data[offset + 2], data[offset + 3]);
	}
 
	public static void main(String[] args) {
		byte[] data = new byte[4];
		data[3] = 35;
		int num = byteToInt(data, 0);
		System.out.println(num);
	}
 
}